#include "Program.h"

time_t duration;

Program::Program(std::string fileName)
{
	algorithm = NULL;
	std::ifstream file(fileName.c_str());
	if(file.is_open())
	{
		file>>size;
		size++;
		file>>machines;
		states = new State*[size+1]();
		states[0] = new State[machines]();
		for(int i =1;i<size;i++)
		{
			states[i]  = new State[machines]();
			states[i][0].id = i;
			
			for(int j = 0;j<machines;j++)
			{
				file>>states[i][j].p;
				states[i][0].value+=states[i][j].p;
			}
		}
		states[size] = new State[machines]();
		Run();
	}
	else
	{
		std::cout<<"Bad file name or file not exists."<<std::endl;
	}
}

void Program::Run()
{

	algorithm = new Algorithm(states,size,machines);
	time_t start,stop;
	time(&start);
	//algorithm->Neh();
	algorithm->NehAcc();
	time(&stop);
	std::cout<<"Size : "<<size-1;
	duration += (stop - start);
	std::cout<<" Duration : "<<duration<<" s cMax : "<<
		algorithm->GetCmax()<<std::endl;
	/*for(int x=1;x<size;x++)
		std::cout<<algorithm->h[x][0].id<<" ";
	std::cout<<std::endl;*/
}

Program::~Program()
{	
	for(int i=1;i<size;i++)
	{
		delete [] states[i];
	}

	if(algorithm!=NULL)
	{
		algorithm->~Algorithm();
	}

}